package com.mobilottery.server;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

/**
 * @author fedor
 * Provides user management functions
 */
public class UserManager {

    static UserManager instance = new UserManager();
    static Logger log = Logger.getLogger(UserManager.class);

    public static UserManager getInstance() {
        return instance;
    }


    /**
     * Registers new user in the system
     * If user already exists throws persistence exception
     * @param user
     */
    public void registerUser(User user) 
    {
        PersistenceManager.getInstance().persist(user);
    }

    /**
     * Checks whether the user with the given name is registered in the system
     * @param userName
     * @return
     */
    public boolean userExists(String userName) {
        //first check if the user is already exists

        //create an sql string for selecting the passed username to check there is such
        String hql = "select count(g) from User as g where g.userName = :UserName";
        //create a map object
        Map<String, Object> parameters = new HashMap<String, Object>();
        //set parameter username from the request
        parameters.put("UserName", userName);
        long userCount = PersistenceManager.getInstance().selectParamSingleQuery(hql, parameters);
        return userCount>0; 
    }    

    /**
     * Updates user record
     * @param user
     */
    public void updateUser(User user) 
    {
        PersistenceManager.getInstance().persist(user);
    }

    /**
     * Removes the user record from the system
     * @param user
     */
    public void deleteUser(User user) 
    {
     	User persistentUser =  getUser(user.getUserName());
    	PersistenceManager.getInstance().delete(persistentUser);
    }


    /**
     * Sets block flag to the user record, prohibiting future logins by this user name
     * @param user
     */
    public void setUserBlocked(User user, boolean isBlocked) 
    {
    	User persistentUser =  getUser(user.getUserName());
    	persistentUser.setIsBlocked(isBlocked);
        PersistenceManager.getInstance().persist(persistentUser);
    }
    
    /**
     * Returns true if user/password match a registered user record in the system
     * @param username
     * @param pass
     * @return
     */
    public boolean isLoginMatch(String username, String pass)   
    {
    	User user = null;
    	try    	
    	{
    		user = getUser(username);
    	}
    	catch(Exception ex)
    	{    		
    		return false;
    	}
    	return User.getPasswordDigest(username, pass).equals(user.getPassword());
    }
    
    /**
     * Retrieves user record by the user name
     * @param username
     * @return
     */
    public User getUser(String username)
    {
        String hql = "select g from User as g where g.userName=:UserName";
        //create a map object
        Map<String, Object> parameters = new HashMap<String, Object>();
        //set parameter username from the request
        parameters.put("UserName", username);
        User user = PersistenceManager.getInstance().selectParamSingleQuery(hql, parameters);
        return user;    	
    }

    /**
     * List game users in the system
     * @return
     */
    public List<User> getUsers() 
    {
        //create an sql string for selecting the passed username to check there is such
        String hql = "select g from GameUser as g";
        List<User> users = PersistenceManager.getInstance().selectQuery(hql);
        return users; 
    }
    /**
     * List administrators in the system
     * @return
     */
    public List<User> getAdminUsers() 
    {
        //create an sql string for selecting the passed username to check there is such
        String hql = "select g from Admin as g";
        List<User> users = PersistenceManager.getInstance().selectQuery(hql);
        return users; 
    }        
    
    /**
     * Tells if administrator users are defined in the system
     * @return
     */
    public Boolean hasAdminUsers() 
    {
        //create an sql string for selecting the passed username to check there is such
        String hql = "select count(g) from Admin as g";
        return ((Long)PersistenceManager.getInstance().selectSingleQuery(hql))>0;
    }       
    
    /**
     * List administrators in the system
     * @return
     */
    public List<User> getAllUsers() 
    {
        //create an sql string for selecting the passed username to check there is such
        String hql = "select g from User as g";
        List<User> users = PersistenceManager.getInstance().selectQuery(hql);
        return users; 
    }
    
    public static void main(String args[])
    {
    	System.out.println("test login: "  + UserManager.getInstance().isLoginMatch("test", "test"));
    }
    
}
